/**
 * 机构数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.OrgGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,
	
	initComponent: function(){
		var gridStructure = [{ 
			name: 'orgId',
			hidden: true
		},{
			name: 'orgCode',  
			header: "机构编号",
			sortable: true,
			width: 20
		},{
			name: 'orgName',  
			header: "机构名称",
			sortable: true,
			width: 30
		},{
			name: 'orgPath',  
			header: "机构位置",
			width: 60
		},{
			name: 'orgTypeLabel',  
			header: "机构类型",
			sortable: true,
			width: 20
		},{
			name: 'orgGradeLabel',  
			header: "机构等级",
			sortable: true,
			width: 20
		},{
			name: 'linkMan',  
			header: "联系人",
			sortable: true,
			width: 20
		},{
			name: 'linkTel',  
			header: "联系电话",
			sortable: true,
			width: 20
		},{
			name: 'orgStatusLabel',  
			header: "机构状态",
			sortable: true,
			width: 20
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'orgCode';
		this.keyField = 'orgId';

		this.deleteMsg = '确认要删除选中的机构吗？<br/><font color="red">注意</font>：删除机构将同时删除该机构下的所有子机构、岗位以及用户关联信息。';
		this.deleteMsgBoxWidth = 490;
		
		Ext.ux.grid.OrgGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(orgGrid){
			var record = orgGrid.getSelectionModel().getSelected();
				if(record != null){
					var orgId = record.get('orgId');
					this.showOrgInfo(orgId);
				}
			});
		}
		
		
		this.on('updateRecord', function(orgId){
			this.showOrgInfo(orgId);
		});
		
		this.on('deleteRecord', function(grid, orgIds){
			this.deleteOrg(orgIds);
		});
	},
	
	showOrgInfo: function(orgId){
		var win = new Ext.ux.org.omorg.OrgDetailWindow({id:'orgDetailWindow', title:'修改机构信息', orgId:orgId});
		win.showOrgInfo();
	},
	
	addTopOrg: function(){
		var win = new Ext.ux.org.omorg.OrgDetailWindow({id:'orgDetailWindow', title:'新增顶级机构'});
		win.show();
	},
	
	addChildOrg: function(parentOrgId, parentOrgName){
		var win = new Ext.ux.org.omorg.OrgDetailWindow({id:'orgDetailWindow', title:'新增下级机构', parentOrgId:parentOrgId, parentOrgName:parentOrgName});
		win.show();
	},
	
	deleteOrg: function(orgIds) {
		Ext.Ajax.request({
			url:'om-org!delete.action',
			success: function(){
				Ext.getCmp('orgTree').refreshOrg();
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除机构失败！');
			},
			method: 'POST',
			params:{
				orgIds: orgIds
			},
			waitMsg : '正在删除数据，稍候...',
			scope: this
		});
	}
});